//
//  MTClubActiviteDetailViewController.swift
//  MT
//
//  Created by ooops on 2019/12/26.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import Eureka
import MTCategory
import RxSwift
import RxCocoa


class MTClubActiviteDetailViewController: BaseFormViewController {
    
    private var group: JMSGGroup!
    private var isEditMode: Bool = false
    private let provider: MTProvider = MTProvider<MTClubApi>()
    private let userProvider: MTProvider = MTProvider<UserCenterApi>()
    private var activityId: String?
    fileprivate lazy var users: [JMSGUser?] = []
    /// 数据模型
    private var model: MTClubActivitiesDetailModel?
    typealias detailUpdateClosure = (_ activityId: String) -> Swift.Void
    
    private var detailUpdateCallback: detailUpdateClosure?
    
    
    /// 是否是创建俱乐部活动
    private var isCreateContet: Bool = false
    
    deinit {
        debugPrint("deinit")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = isCreateContet ? "我的活动" : "活动详情"
        
        tableView.backgroundColor = UIColor.theme.background
        tableView.separatorStyle = .none
        tableView.contentInset = UIEdgeInsets(top: 40, left: 0, bottom: 0, right: 0)
        setupForm(isCreateContet)
        view.addSubview(commitButton)
        /// 创建活动下显示按钮
        self.commitButton.isHidden = !isCreateContet
        
        commitButton.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20)
            make.width.equalTo(210)
            make.height.equalTo(44)
        }
        /// 非编辑 / 创建模式 填充数据
        if isCreateContet == false {
            parseData(self.model)
            loadActivityDetail()
        }
        
        group.memberInfoList({ (result, error) in
            if let userInfos: [JMSGGroupMemberInfo] = result as? [JMSGGroupMemberInfo] {
                self.users = userInfos.map { (info) -> JMSGUser? in
                    return info.user
                }
            }
        })
    }
    
    @objc private func _updateGroupInfo() {
        group.memberInfoList({ (result, error) in
            if let userInfos: [JMSGGroupMemberInfo] = result as? [JMSGGroupMemberInfo] {
                self.users = userInfos.map { (info) -> JMSGUser? in
                    return info.user
                }
            }
        })
    }
    
    /// 是否编辑内容
    /// 编辑模式 输入框默认填充 接口取回来的 的数据
    /// 信息查看模式
    /// - Parameter group: group description
    convenience init(group: JMSGGroup, activityId: String, _ complete: detailUpdateClosure? = nil) {
        self.init()
        self.group = group
        self.isCreateContet = false
        self.isEditMode = false
        self.activityId = activityId
        self.detailUpdateCallback = complete
    }
    
    /// 是否编辑内容
    /// 编辑模式 输入框默认填充 接口取回来的 的数据
    /// 信息查看模式
    /// - Parameter group: group description
    convenience init(group: JMSGGroup, isEditMode: Bool, activityId: String, _ complete: detailUpdateClosure? = nil) {
        self.init()
        self.group = group
        self.isCreateContet = false
        self.isEditMode = isEditMode
        self.activityId = activityId
        self.detailUpdateCallback = complete
    }
    
    /// 是否创建内容
    /// 创建模式 输入框没有默认值
    /// 信息查看模式
    /// - Parameter group: group description
    convenience init(group: JMSGGroup, isCreateContet: Bool, _ complete: detailUpdateClosure? = nil) {
        self.init()
        self.group = group
        self.isCreateContet = isCreateContet
        self.isEditMode = isCreateContet
        self.detailUpdateCallback = complete
    }
    
    @objc private func commitClub() {
        
        /// 申请加入俱乐部的
        if group.owner != JMSGUser.myInfo().username {
            guard let activityId = self.activityId else { return }
            
            MTClubTool.enroll(provider: provider, requestParam: ["activityId" : activityId], complete: { [weak self] (model) in
                guard let weakSelf = self else { return }
                if model?.code == 200 {
                    MBProgressHUD_JChat.show(text: "报名成功", view: weakSelf.view)
                    weakSelf.pop()
                    weakSelf.detailUpdateCallback?(activityId)
                }
            }) { (error) in
                
            }
            return
        }
        
        /// 创建俱乐部
        if self.isCreateContet {
            self.commitClubActivities()
            return
        }
        
        /// 编辑俱乐部 - 应该需要另外一个接口?
        self.commitClubActivities(true)
    }
    
    private func showValidationMsg(msgs: [ValidationError]) {
        MBProgressHUD_JChat.show(text: msgs.first!.msg, view: self.view)
    }
    
    
    /// 修改报名按钮状态
    private func enableEnroll() {
        /// 群主可以修改细信息
        if group.owner == JMSGUser.myInfo().username {
            navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightEditButton)
        } else {
            /// 非俱乐部拥有者, 只能报名参加活动
            commitButton.isHidden = false
            if model?.result?.clubActivityVo?.joined == true {
                commitButton.isUserInteractionEnabled = false
                commitButton.setTitle("已报名", for: .normal)
                commitButton.backgroundColor = UIColor.redpack.x5d5d5d
            } else {
                commitButton.isUserInteractionEnabled = true
                commitButton.setTitle("报名", for: .normal)
                commitButton.backgroundColor = UIColor.theme.main
            }
        }
    }
    
    /// 编辑俱乐部
    @objc private func editClubInfo() {
        if isEditMode {
            return
        }
        /// 编辑模式下才显示按钮
        self.commitButton.isHidden = false
        isEditMode = true
        form.removeAll()
        setupForm(true)
        parseData(self.model)
        self.validatorDataDictionary()
    }
    
    private func loadActivityDetail() {
        guard let activityId = self.activityId else { return }
        MTClubTool.queryActivityInfo(provider: provider, requestParam: ["activityId" : activityId], complete: { [weak self] (model) in
            guard let weakSelf = self else { return }
            
            weakSelf.model = model
            DispatchQueue.main.async {
                weakSelf.parseData(weakSelf.model)
            }
            
            weakSelf.enableEnroll()
            
        }) { (error) in
            
        }
    }
    
    private func parseData(_ model: MTClubActivitiesDetailModel?) {
        if let m = model {
            if let remark = m.result?.clubActivityVo?.remark {
                setValue(value: remark, forTag: clubDescriptionKey)
            }
            
            if let name = m.result?.clubActivityVo?.name {
                setValue(value: name, forTag: clubNameKey)
            }
            
            if let date = m.result?.clubActivityVo?.startTime {
                if let row: MTClubDateRow = self.form.rowBy(tag: clubDateKey) as? MTClubDateRow {
                    row.value = DateManager.date(dateString: date, format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
                    let formatter = DateFormatter()
                    formatter.locale = .current
                    formatter.dateFormat = "yyyy-MM-dd"
                    row.dateFormatter = formatter
                    row.updateCell()
                }
            }
            
            if let num = m.result?.clubActivityVo?.num {
                setValue(value: "\(num)", forTag: clubPeopleKey)
            }
            
            if let squ: String = m.result?.clubActivityVo?.address {
                setValue(value: squ, forTag: clubAddrKey)
            }
            
            if let row: MTClubActivityDetailMemberRow = self.form.rowBy(tag: clubMemberKey) as? MTClubActivityDetailMemberRow {
                    //row.cell.group = self.group
                if let userList = self.model?.result?.userList {
                    /// 所有的用户名
                    let userNames = userList.map { (m) -> String in
                        return m.userName ?? ""
                    }.filter { (x) -> Bool in
                        return x != ""
                    }
                    /// 转化成极光的用户对象
                    JMSGUser.userInfoArray(withUsernameArray: userNames) { [weak self] (result, error) in
                        guard let weakSelf = self else { return }
                        if error == nil {
                            let users = result as! [JMSGUser]
                            if let joined = weakSelf.model?.result?.clubActivityVo?.joined {
                                row.cell.isJoinGroupActivitity = joined
                            }
                            if weakSelf.group != nil {
                                row.cell.group = weakSelf.group
                            }
                            row.cell.users = users
                            row.updateCell()
                        }
                    }
                } else {
                    row.cell.users = users
                    row.updateCell()
                }
            }
            
        } else {
            if let name = group.name {
                setValue(value: name, forTag: clubNameKey)
            }
            setValue(value: "", forTag: clubPeopleKey)
            setValue(value: DateManager.string(date: Date(), format: "yyyy-MM-dd"), forTag: clubDateKey)
            setValue(value: "", forTag: clubAddrKey)
            setValue(value: "", forTag: clubDescriptionKey)
            setValue(value: "", forTag: clubMemberKey)
        }
    }
    
    /// 赋值
    /// - Parameters:
    ///   - value:
    ///   - tag:
    private func setValue(value: String, forTag tag: String) {
        switch tag {
        case clubNameKey, clubPeopleKey:
            if let row: MTClubDetailTextRow = self.form.rowBy(tag: tag) as? MTClubDetailTextRow {
                row.cell.contentString = value
                row.value = value
                row.reload()
                row.cellUpdate { (cell, row) in
                    row.cell.isHiddenAccessory = true
                    cell.height = { UITableView.automaticDimension }
                }
            }
        case clubAddrKey:
            if let row: MTClubAreaRow = self.form.rowBy(tag: tag) as? MTClubAreaRow {
                row.value = value
                row.reload()
                row.cellUpdate { (cell, row) in
                    
                    cell.height = { UITableView.automaticDimension }
                }
            }
        case clubDescriptionKey:
            if let row: MTClubDetailTextRow = self.form.rowBy(tag: tag) as? MTClubDetailTextRow {
                row.cell.contentString = value
                row.value = value
                row.reload()
                row.cellUpdate { (cell, row) in
                    cell.height = { UITableView.automaticDimension }
                }
            }
        case clubMemberKey:
            if let row: MTClubActivityDetailMemberRow = self.form.rowBy(tag: tag) as? MTClubActivityDetailMemberRow {
                    //row.cell.group = self.group
                if let userList = self.model?.result?.userList {
                    /// 所有的用户名
                    let userNames = userList.map { (m) -> String in
                        return m.nickname ?? ""
                    }.filter { (x) -> Bool in
                        return x != ""
                    }
                    /// 转化成极光的用户对象
                    JMSGUser.userInfoArray(withUsernameArray: userNames) { [weak self] (result, error) in
                        guard let weakSelf = self else { return }
                        if error == nil {
                            let users = result as! [JMSGUser]
                            if let joined = weakSelf.model?.result?.clubActivityVo?.joined {
                                row.cell.isJoinGroupActivitity = joined
                            }
                            if weakSelf.group != nil {
                                row.cell.group = weakSelf.group
                            }
                            row.cell.users = users
                            row.updateCell()
                        }
                    }
                } else {
                    row.cell.users = users
                    row.updateCell()
                }
            }
        default:
            break
        }
    }
    
    /// 配置俱乐部活动表格
    /// - Parameter isEditMode: 是否编辑模式 / 创建模式
    private func setupForm(_ isEditMode: Bool = false) {
        
        form +++ Section()
            <<< MTClubDetailTextRow(){ row in
                row.cell.tipString = "名称"
                row.cell.contentString = ""
                row.cell.maxTextLength = 10
                row.cell.placeholder = "不能多于10个中文字"
                row.cell.isEditMode = isEditMode
                row.add(rule: MTClubNameRule())
                row.tag = clubNameKey
                row.updateCell()
            }.onChange({ (row) in
                self.validatorDataDictionary()
            })
            
            <<< MTClubDetailTextRow(){ row in
                row.cell.tipString = "人数"
                row.cell.contentString = ""
                row.cell.placeholder = "输入活动人数"
                row.cell.isHiddenAccessory = !isEditMode
                row.cell.txfKeyboardType = .numberPad
                row.cell.isEditMode = isEditMode
                row.tag = clubPeopleKey
                row.updateCell()
            }.onChange({ (row) in
                self.validatorDataDictionary()
            })
            
            <<< MTClubDateRow() { row in
                row.cell.tipString = "时间"
                row.cell.isHiddenAccessory = false
                row.cell.contentString = ""
                row.cell.isEditMode = isEditMode
                row.add(rule: RuleRequired())
                row.tag = clubDateKey
                row.value = Date()
                let formatter = DateFormatter()
                formatter.locale = .current
                formatter.dateFormat = "yyyy年MM月dd日HH时mm分"
                row.dateFormatter = formatter
                /// 最早三天后的活动
                row.minimumDate = row.value?.addingTimeInterval(60*60*24)
                ///
                row.maximumDate = row.value?.addingTimeInterval(60 * 60 * 24 * 30 * 12 * 100)
                
            }.onChange({ (row) in
                self.validatorDataDictionary()
            })
            
            <<< MTClubAreaRow() { row in
                row.cell.tipString = "地址"
                row.cell.isEditMode = isEditMode
                row.tag = clubAddrKey
            }.onChange({ (row) in
                self.validatorDataDictionary()
            })
            
            <<< MTClubDetailTextRow(){ row in
                row.cell.tipString = "活动简介"
                row.cell.contentString = ""
                row.cell.contentType = .editTextMutiply
                row.cell.isHiddenAccessory = true
                row.cell.isEditMode = isEditMode
                row.cell.maxTextLength = 50
                row.cell.placeholder = "快来给大家介绍下新的活动主题吧"
                row.tag = clubDescriptionKey
                row.updateCell()
            }.onChange({ (row) in
                self.validatorDataDictionary()
            })
            
            <<< MTClubActivityDetailMemberRow(){ row in
                row.tag = clubMemberKey
                row.hidden = Condition(booleanLiteral: isEditMode || isCreateContet)
                row.cell.delegate = self
                if self.group != nil {
                    row.cell.group = self.group
                }
                if let userList = self.model?.result?.userList {
                    /// 所有的用户名
                    let userNames = userList.map { (m) -> String in
                        return m.nickname ?? ""
                    }.filter { (x) -> Bool in
                        return x != ""
                    }
                    /// 转化成极光的用户对象
                    JMSGUser.userInfoArray(withUsernameArray: userNames) { [weak self] (result, error) in
                        guard let weakSelf = self else { return }
                        if error == nil {
                            let users = result as! [JMSGUser]
                            if let joined = weakSelf.model?.result?.clubActivityVo?.joined {
                                row.cell.isJoinGroupActivitity = joined
                            }
                            if weakSelf.group != nil {
                                row.cell.group = weakSelf.group
                            }
                            row.cell.users = users
                            row.updateCell()
                        }
                    }
                }
            }
        
        GroupTableSeparatorFix.removeFirstAndLastSeparatorLine()
    }
    
    lazy var commitButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("保存并发起", for: .normal)
        btn.setTitleColor(UIColor.text.x161616, for: .normal)
        btn.backgroundColor = UIColor.redpack.x5d5d5d
        btn.isUserInteractionEnabled = false
        btn.layer.cornerRadius = 22
        btn.layer.masksToBounds = true
        btn.addTarget(self, action: #selector(commitClub), for: .touchUpInside)
        btn.isHidden = true
        return btn
    }()
    
    lazy var rightEditButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("修改信息", for: .normal)
        btn.addTarget(self, action: #selector(editClubInfo), for: .touchUpInside)
        btn.setTitleColor(UIColor.redpack.x5d5d5d, for: .normal)
        btn.titleLabel?.font = PingFangSCRegularFont(14)
        return btn
    }()
    
    // MARK: - 检查数据有效性
    private func validatorDataDictionary() {
        
        let validationErrors: [ValidationError] = self.form.validate()
        if validationErrors.count != 0 {
            commitButton.isUserInteractionEnabled = false
            commitButton.backgroundColor = UIColor.redpack.x5d5d5d
            return
        } else {
            /// 编辑模式修改
            if isEditMode {
                commitButton.isUserInteractionEnabled = true
                commitButton.setTitle("保存", for: .normal)
                commitButton.backgroundColor = UIColor.theme.main
            } else {
                if model?.result?.clubActivityVo?.joined == true {
                    commitButton.isUserInteractionEnabled = false
                    commitButton.setTitle("已报名", for: .normal)
                    commitButton.backgroundColor = UIColor.redpack.x5d5d5d
                } else {
                    commitButton.isUserInteractionEnabled = true
                    commitButton.setTitle("报名", for: .normal)
                    commitButton.backgroundColor = UIColor.theme.main
                }
            }
        }
    }
    
    @objc private func commitClubActivities(_ isEdit: Bool = false) {
        
        let validationErrors: [ValidationError] = self.form.validate()
        if validationErrors.count != 0 {
            self.showValidationMsg(msgs: validationErrors)
            return
        }
        
        let values = self.form.values()
        
        var requestParam: [String : Any] = [
            "address": "",
            "area": "",
            "city": "",
            "pushId": self.group.gid,
            "endTime": "",
            "enrollNum": 0,
            "name": "",
            "num": 0,
            "province": "",
            "remark": "",
            "startTime": ""
        ]
        
        if let startTime = values[clubDateKey] as? Date, let sts: String = DateManager.string(date: startTime, format: "yyyy-MM-dd HH:mm:ss") {
            requestParam.updateValue(sts, forKey: "startTime")
        }
        
        if let remark = values[clubDescriptionKey] as? String {
            requestParam.updateValue(remark, forKey: "remark")
        }
        
        if let number = values[clubPeopleKey] as? String, let num = Int(number) {
            requestParam.updateValue(num, forKey: "num")
        }
        
        if let name = values[clubNameKey] as? String {
            requestParam.updateValue(name, forKey: "name")
        }
        
        if let address = values[clubAddrKey] as? String {
            requestParam.updateValue(address, forKey: "address")
        }
        
        if let squ: String = values[clubAddrKey] as? String {
            let array: [Substring] = squ.split(separator: "-")
            requestParam.updateValue(array[2], forKey: "area")
            requestParam.updateValue(array[1], forKey: "city")
            requestParam.updateValue(array[0], forKey: "province")
        }
        
        
        if let clubActivityId = self.model?.result?.clubActivityVo?.id, isEdit {
            requestParam.updateValue(clubActivityId, forKey: "id")
            MTClubTool.editClubActivity(provider: provider, requestParam: requestParam, complete: { [weak self] (model) in
                guard let weakSelf = self else { return }
                if model?.code == 200 {
                    MBProgressHUD_JChat.show(text: "修改成功", view: weakSelf.view)
                } else {
                    MBProgressHUD_JChat.show(text: "修改失败", view: weakSelf.view)
                }
                DispatchQueue.main.after(1) {
                    weakSelf.pop()
                }
                
            }) { (error) in
                
            }
            return
        }
        
        /// 查询创建俱乐部活动所需要消耗的钻石
        MTClubTool.config(provider: provider, complete: {[weak self] (model) in
            guard let weakSelf = self else { return }
            guard let activityMoney = model?.result?.activityMoney else { return }
            //guard let userMoney = model?.result?.moeny else { return }
            let userMoney = model?.result?.moeny ?? 0
            /// 查询用户余额
            let wechatOfficeAccount = model?.result?.wechatOfficeAccount ?? ""
            if model?.code == 200, userMoney < activityMoney || model?.code == 600 {
                // 发起专属俱乐部活动需花费\(activityMoney)钻石您当前余额不够，是否去充值
                UIAlertController.rx.show(in: weakSelf, title: "提示", message: "创建活动需要\(activityMoney)钻石，钻石不足，请充值", buttons: [.cancel("取消"), .default("去充值")]).subscribe(onSuccess: { index in
                    /// 去充值的引导
                    if index == 1 {
                        // let pas = UIPasteboard.general
                        // pas.string = wechatOfficeAccount
                        // MBProgressHUD_JChat.show(text: "微信号复制成功", view: weakSelf.view)
                    }
                })
                .disposed(by: globalDisposedBag)
            } else {
                /// 跳转去创建俱乐部活动
                UIAlertController.rx.show(in: weakSelf, title: "提示", message: "发起专属俱乐部活动需花费\(activityMoney)钻石是否去发起?", buttons: [.cancel("取消"), .default("去发起")]).subscribe(onSuccess: { [weak self] index in
                    guard let weakSelf = self else { return }
                    if index == 1 {
                        MTClubTool.createClubActivities(provider: weakSelf.provider, requestParam: requestParam, complete: { [weak self] (model) in
                            guard let weakSelf = self else { return }
                            if model?.code == 200 {
                                weakSelf.pop()
                            }
                        }) { (error) in
                            
                        }
                    }
                })
                .disposed(by: globalDisposedBag)
            }
            
        }) { (error) in
            
        }
    }
}

extension MTClubActiviteDetailViewController: MTClubActivityDetailMemberCellDelegate {
    func clickMoreButton(clickButton button: UIButton) {
        
        if let userList = self.model?.result?.userList {
            /// 所有的用户名
            let userNames = userList.map { (m) -> String in
                return m.userName ?? ""
            }.filter { (x) -> Bool in
                return x != ""
            }
            /// 转化成极光的用户对象
            JMSGUser.userInfoArray(withUsernameArray: userNames) { (result, error) in
                if error == nil {
                    if let users = result as? [JMSGUser] {
                        let vc = MTMemberViewController(users: users)
                        DispatchQueue.main.async {
                            self.navigationController?.pushViewController(vc, animated: true)
                        }
                    }
                    
                }
            }
        }
    }
    
    func clickAddCell() {
        var uns: [String] = []
        if let userNames = self.model?.result?.userList {
            
            for item in userNames {
                if let nickName = item.userName {
                    uns.append(nickName)
                }
            }
        }
        
        /// 转化成极光的用户对象
        JMSGUser.userInfoArray(withUsernameArray: uns) { [weak self] (result, error) in
            guard let weakSelf = self else { return }
            if error == nil {
                if let users = result as? [JMSGUser] {
                    let vc = JCUpdateMemberViewController(updateMemberType: .clubActivity, joinUsers: users, complete: { (addUserNames) in
                        
                        guard let activityId = weakSelf.activityId else { return }
                        
                        weakSelf.users = weakSelf.users + addUserNames
                        
                        var uns: [String] = []
                        for item in addUserNames {
                            uns.append(item.username)
                        }
                        
                        let requestParam : [String : Any] = ["activityId" : activityId, "type" : 1, "users" : uns]
                        
                        MTClubTool.operateClubUsers(provider: weakSelf.provider, operateType: .clubActivity, requestParam: requestParam, complete: { [weak self] (model) in
                            guard let weakSelf = self else { return }
                            /// 如果成功添加活动用户, 则刷新数据
                            if model?.code == 200 {
                                if let row: MTClubActivityDetailMemberRow = weakSelf.form.rowBy(tag: clubMemberKey) as? MTClubActivityDetailMemberRow {
                                    row.cell.users = weakSelf.users
                                    row.updateCell()
                                }
                                
                                weakSelf.loadActivityDetail()
                            }
                            
                        }) { (error) in
                            
                        }
                    })
                    vc.group = weakSelf.group
                    DispatchQueue.main.async {
                        weakSelf.navigationController?.pushViewController(vc, animated: true)
                    }
                }
            }
        }
    }
    
    func clickRemoveCell() {
        
        var uns: [String] = []
        if let userNames = self.model?.result?.userList {
            
            for item in userNames {
                if let nickName = item.userName {
                    uns.append(nickName)
                }
            }
        }
        
        /// 转化成极光的用户对象
        JMSGUser.userInfoArray(withUsernameArray: uns) { [weak self] (result, error) in
            guard let weakSelf = self else { return }
            if error == nil {
                if let users = result as? [JMSGUser] {
                    let vc = JCRemoveMemberViewController(updateMemberType: .clubActivity, removeUsers: users) { [weak self] (removeUserNames) in
                        guard let weakSelf = self else { return }
                        guard let activityId = weakSelf.activityId else { return }
                        
                        weakSelf.users.removeAll { (user) -> Bool in
                            return removeUserNames.contains { (rmUser) -> Bool in
                                return rmUser.isEqual(to: user)
                            }
                        }
                        
                        var uns: [String] = []
                        for item in removeUserNames {
                            uns.append(item.username)
                        }
                        
                        let requestParam : [String : Any] = ["activityId" : activityId, "type" : 2, "users" : uns]
                        
                        MTClubTool.operateClubUsers(provider: weakSelf.provider, operateType: .clubActivity, requestParam: requestParam, complete: { (model) in
                            /// 移除俱乐部活动用户成功,  更新数据
                            if model?.code == 200 {
                                if let row: MTClubActivityDetailMemberRow = weakSelf.form.rowBy(tag: clubMemberKey) as? MTClubActivityDetailMemberRow {
                                    row.cell.users = weakSelf.users
                                    row.updateCell()
                                }
                                
                                weakSelf.loadActivityDetail()
                                
                                /// 更新对应模型数据
//                                if let userList = weakSelf.model?.result?.userList {
//                                    let uLModels = userList.filter { (m) -> Bool in
//                                        return removeUserNames.contains { (juser) -> Bool in
//                                            if let uname = m.userName {
//                                                return juser.username == uname
//                                            }
//                                            return false
//                                        }
//                                    }
//
//                                    weakSelf.model?.result?.userList = uLModels
//                                }
                            }
                            
                        }) { (error) in
                            
                        }
                    }
                    vc.group = weakSelf.group
                    weakSelf.navigationController?.pushViewController(vc, animated: true)
                }
            }
        }
    }
    
//    func didSelectCell(indexPath: IndexPath) {
//        let index = indexPath.section * 5 + indexPath.row
//        if let user = users[index] {
////            if user.isEqual(to: JMSGUser.myInfo()) {
////                navigationController?.pushViewController(JCMyInfoViewController(), animated: true)
////                return
////            }
////            let vc = JCUserInfoViewController()
////            vc.user = user
//            let vc = MineViewController(username: user.username)
//            navigationController?.pushViewController(vc, animated: true)
//        }
//    }
    
    func didSelectCell(userName: String) {
        let vc = MineViewController(username: userName)
        navigationController?.pushViewController(vc, animated: true)
    }
    
}

extension MTClubActiviteDetailViewController {
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    
}
